<template>
    <div class="page">
        <van-list v-model="loading" :finished="finished" @load="onLoad">
            <news-right-icon v-for="(item, key) in dataList" :key="key" :data="item" @onclick="handleToDetail(item.id)"/>
        </van-list>
    </div>
</template>

<style lang="less" scoped>
    .page {
        .search-view {
            /*margin-top: 4.3rem;*/
        }
        .swiper-box {
            margin: .9rem;
        }

    }
</style>

<script>

    import {study} from 'api';

    export default {
        data() {
            return {
                dataList: [],
                loading: false,
                finished: false,
                pageSize: 10,
                pageNumber: 1
            }
        },
        created() {
            // this.requestData();
        },
        methods: {
            requestData(type = this.$route.params.type) {
                const typeObj = {
                    type1: 3,
                    type2: 5,
                    type3: 1,
                    type4: 2,
                    type5: 5,
                    type6: 7,
                };

                this.loading= true;
                this.$axios.get(study.API_INFORMATION, {
                    params: {
                        type: typeObj[type],
                        pageSize: this.pageSize,
                        pageNumber: this.pageNumber,
                    }
                }).then(({data}) => {
                    this.loading= false;

                    let {list} = data;
                    this.finished = !list.length;
                    list = list.map(o => ({
                        title: o.title,
                        date: new Date(o.createtime).toAppTime(true),
                        src: o.imgpath,
                        id: o.id
                    }));
                    this.dataList = this.dataList.concat(list);
                }).catch(()=>{
                    this.loading= false;
                })
            },
            handleToDetail(id) {
                console.log(id)
                this.$router.push('/study/detail/' + id);
            },
            onLoad() {
                this.pageNumber += 1;
                // this.requestData();
            }
        },
        beforeRouteUpdate(to, from, next) {
            this.dataList = [];
            this.pageNumber = 1;
            this.finished = false;
            this.loading = false;
            this.requestData(to.params.type);
            next();
        }
    }
</script>